﻿using System;
using System.Configuration;
using System.IO;
using ElectronicCommerce.Framework.Ioc.Interceptor.Configuration;
using ElectronicCommerce.Framework.Utility;

namespace ElectronicCommerce.Framework.Ioc.Windsor
{
    /// <summary>
    /// Intercepter ConfigHelper
    /// </summary>
    internal static class IntercepterConfigHelper
    {
        internal static IntercepterConfiguration IntercepterConfig
        {
            get;
            private set;
        }

        internal static void CreateInercepetors()
        {
            string file = ConfigurationManager.AppSettings["Intercepter"];
            if (string.IsNullOrEmpty(file))
            {
                return;
            }
            file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                file.Replace('/', '\\').TrimStart('\\'));

            if (!File.Exists(file))
            {
                return;
            }

            IntercepterConfiguration config = ObjectXmlSerializer.LoadFromXml<IntercepterConfiguration>(file);


            if (config == null || config.IntercepterInfos == null
                || config.IntercepterInfos.Count == 0)
            {
                return;
            }

            IntercepterConfig = config;
        }
    }
}
